package com.com.gmail.at.vssabinin.client;

import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;

import DTO.PassengerDTO;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

public class Registration extends javax.swing.JDialog {

	private static final long serialVersionUID = 1L;
	
    public Registration(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        setResizable(false);
        initComponents();
    }

    public static Registration regInstance = 
            new Registration(Main.mainInstance, true);
 
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        name = new javax.swing.JTextField();
        register = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        surname = new javax.swing.JTextField();
        dateOfBirth = new javax.swing.JTextField();
        login = new javax.swing.JTextField();
        password = new javax.swing.JTextField();
        passwordRepeate = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        
        this.addWindowListener( 
            	new WindowAdapter() { 
            		public void windowClosing(WindowEvent e) { 
            			exit(); 
            	    } 
            	} 
        );

        jLabel1.setText("Please fill in this form");

        jLabel2.setText("Name");

        name.setText("Ivan");

        register.setText("Register");
        
        register.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                registerMouseReleased(evt);
            }
        });
        
        JButton goBack = new JButton("go back");
        
        goBack.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                goBackMouseReleased(evt);
           }
        });

        jLabel3.setText("Surname");

        jLabel4.setText("Date of birth");

        jLabel5.setText("Login");

        jLabel6.setText("Password");

        jLabel7.setText("Repeate it");

        surname.setText("Ivanov");

        dateOfBirth.setText("21.12.2012");

        login.setText("qwerty");

        password.setText("123");

        passwordRepeate.setText("123");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        layout.setHorizontalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        			.addGroup(layout.createParallelGroup(Alignment.LEADING)
        				.addGroup(layout.createSequentialGroup()
        					.addContainerGap()
        					.addGroup(layout.createParallelGroup(Alignment.LEADING)
        						.addGroup(layout.createSequentialGroup()
        							.addGroup(layout.createParallelGroup(Alignment.LEADING)
        								.addComponent(jLabel2)
        								.addComponent(jLabel3)
        								.addComponent(jLabel4)
        								.addComponent(jLabel5)
        								.addComponent(jLabel6)
        								.addComponent(jLabel7))
        							.addGap(38)
        							.addGroup(layout.createParallelGroup(Alignment.LEADING)
        								.addComponent(name, 97, 97, 97)
        								.addComponent(surname, 97, 97, 97)
        								.addComponent(dateOfBirth, 97, 97, 97)
        								.addComponent(login, 97, 97, 97)
        								.addComponent(password, 97, 97, 97)
        								.addComponent(passwordRepeate, 97, 97, 97)))
        						.addGroup(layout.createSequentialGroup()
        							
        							.addPreferredGap(ComponentPlacement.RELATED, 72, Short.MAX_VALUE)
        							)))
        				.addGroup(layout.createSequentialGroup()
        					.addGap(55)
        					.addComponent(jLabel1)
        					.addGap(0, 51, Short.MAX_VALUE))
        				.addGroup(Alignment.TRAILING, layout.createSequentialGroup()
        					.addContainerGap()
        					.addComponent(goBack)
        					.addPreferredGap(ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
        					.addComponent(register)))
        			.addContainerGap())
        );
        layout.setVerticalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        			.addContainerGap()
        			.addComponent(jLabel1)
        			.addGap(18)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel2)
        				.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel3)
        				.addComponent(surname, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel4)
        				.addComponent(dateOfBirth, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel5)
        				.addComponent(login, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel6)
        				.addComponent(password, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel7)
        				.addComponent(passwordRepeate, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(goBack)
        				.addComponent(register))
        			.addContainerGap())
        );
        getContentPane().setLayout(layout);

        pack();
    }

    private void registerMouseReleased(MouseEvent evt) {
        PassengerDTO passenger = new PassengerDTO();
        Calendar calendar = Calendar.getInstance();
        String[] sDate = this.dateOfBirth.getText().split("\\.");
        calendar.set(Integer.valueOf(sDate[2]), Integer.valueOf(sDate[1]), Integer.valueOf(sDate[0]));
        passenger.setPassengerDateOfBirth(calendar.getTime());
        passenger.setLogin(this.login.getText());
        passenger.setPassengerName(this.name.getText());
        passenger.setPassengerSurname(this.surname.getText());
        passenger.setPassword(this.password.getText());
        Main.mainInstance.client.sendThroughProtocol("registration", passenger);
        Protocol.Message message = Main.mainInstance.client.readThroughProtocol();
        String ans = message.getType();
        if (ans.equals("ok")) {
        	Main.mainInstance.passenger = (PassengerDTO) message.getObject();
        	this.dispose();
        	Main.mainInstance.loginViewSet();
        } else if (ans.equals("no")) {
        	JOptionPane.showMessageDialog(null, "This combination of login/name/surname already exists!");
        }
    }
    
    private void goBackMouseReleased(MouseEvent evt) {
    	this.dispose();
    	Login.loginInstance.setVisible(true);
    }
    
    private void exit() {
    	this.dispose();
    	Main.mainInstance.exit();
    }
    
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Registration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Registration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Registration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Registration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
  
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Registration dialog = new Registration(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    private javax.swing.JTextField dateOfBirth;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField login;
    private javax.swing.JTextField name;
    private javax.swing.JTextField password;
    private javax.swing.JTextField passwordRepeate;
    private javax.swing.JButton register;
    private javax.swing.JTextField surname;
}
